package com.jiepos.mpos.biz.transaction.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Code;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.LogChannelStlDao;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.common.entity.App_Log_ChannelStlEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.ExcelUtil;
import com.jiepos.mpos.core.util.StringUtils;

@Service
public class LogChannelStlService extends BaseService {
	/** 导出excel的sheet名称：渠道结算流水*/
	private static final String SHEET_NAME = "渠道结算流水";
	/** 导出表格列头*/
	private static List<String> excelHead = null;

	@Autowired
	private LogChannelStlDao logChannelStlDao;
	
	/**
	 * 查询渠道清算流水分页数据
	 * @param pagination
	 * @param condition
	 * @param customer
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getChannelStlPageList(Pagination pagination, App_Log_ChannelStlEntity condition, App_Custom_InfEntity customer){
		String sql = getChannelStlListSql(condition, customer);
		List<String> params = getChannelStlListParam(condition, customer);
		pagination.setQueryString(sql);
		Map<String, Object> pages = logChannelStlDao.getPaginationMap(pagination, null, params);
		return pages;
	}
	
	public String createExcel(String fileName, App_Log_ChannelStlEntity condition, App_Custom_InfEntity customer){
		List<Map> datas = getChannelStlExportList(condition, customer);
		return ExcelUtil.writeExcel(fileName, SHEET_NAME, excelHead, datas);
	}
	
	/**
	 * 查询渠道清算流水导出列表
	 * @param condition
	 * @param customer
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Map> getChannelStlExportList(App_Log_ChannelStlEntity condition, App_Custom_InfEntity customer){
		String sql = getChannelStlListSql(condition, customer);
		List<String> params = getChannelStlListParam(condition, customer);
		return logChannelStlDao.queryForList(sql, params);
	}
	
	/**
	 * 组装分页与导出查询sql
	 * @param condition
	 * @param customer
	 * @return
	 */
	private String getChannelStlListSql(App_Log_ChannelStlEntity condition, App_Custom_InfEntity customer){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT LC.CHANNELTL_UUID UUID,")
		.append("       TO_CHAR(TO_DATE(LC.LOC_DT || LC.LOC_TS, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') LOC_TIME,")
		.append("       LC.REQ_SN_NO,")
		.append("       LC.LOC_TRACE_NO,")
		.append("       LC.CUSTOM_NO,")
		.append("       substr(CI.LOGIN_ID, 1, 3) || '****' || substr(CI.LOGIN_ID, 8, 4) LOGIN_ID,")
		.append("       CI.NAME CUSTOM_NAME,")
		.append("       NVL2(LC.CHANNEL_NO, LC.CHANNEL_NO || '-' || IC.CHN_NM, '') CHANNEL_NO,")
		.append("       LC.CHANNEL_TRACE_NO,")
		.append("       LC.YEEPAY_SERIALNO,")
		.append("       TO_CHAR(LC.APPLY_AMT, 'FM999999999990.00') APPLY_AMT,")
		.append("       TO_CHAR(LC.FEE, 'FM999999999990.00') FEE,")
		.append("       TO_CHAR(LC.STL_AMT, 'FM999999999990.00') STL_AMT,")
		.append("       TO_CHAR(TO_DATE(LC.STL_DT || LC.STL_TS, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') STL_TIME,")
		.append("       LC.STLST || '-' || CL.CODE_DESC STLST,")
		.append("       LC.RSPCODE || '-' || LC.RSPMSG RSPCODE")
		.append("  FROM ").append(Table.T_APP_LOG_CHANNELSTL).append(" LC")
		.append("  LEFT JOIN ").append(Table.T_APP_CUSTOM_INF).append(" CI")
		.append("    ON LC.CUSTOM_NO = CI.CUSTOM_NO")
		.append("  LEFT JOIN ").append(Table.T_APP_INF_CHANNEL).append(" IC")
		.append("    ON LC.CHANNEL_NO = IC.CHANNEL_NO")
		.append("  LEFT JOIN ").append(Table.T_APP_CODE_LIST).append(" CL")
		.append("    ON LC.STLST = CL.CODE_VAL AND CL.CODE_TP = '").append(Code.STLST).append("'")
		.append(" WHERE 1 = 1");
		/*响应码查询条件*/
		if(SystemConstant.RSPCODE_ERROR.equals(condition.getRspcode())){
			sql.append("   AND LC.RSPCODE <> ?");
			sql.append("   AND LC.RSPCODE <> ?");
		}else if(StringUtils.isNotBlank(condition.getRspcode())){
			sql.append("   AND LC.RSPCODE = ?");
		}
		/*清算状态查询条件*/
		if(StringUtils.isNotBlank(condition.getStlst())){
			sql.append("   AND LC.STLST = ?");
		}
		/*渠道编号查询条件*/
		if(StringUtils.isNotBlank(condition.getChannel_no())){
			sql.append("   AND LC.CHANNEL_NO = ?");
		}
		/*申请起始日期查询条件*/
		sql.append("   AND LC.LOC_DT <= ?");
		/*申请结束日期查询条件*/
		sql.append("   AND LC.LOC_DT >= ?");
		/*会员姓名查询条件*/
		if(StringUtils.isNotBlank(customer.getName())){
			sql.append("   AND CI.NAME LIKE ?");
		}
		/*会员登录账号查询条件*/
		if(StringUtils.isNotBlank(customer.getLogin_id())){
			sql.append("   AND CI.LOGIN_ID LIKE ?");
		}
		/*会员编号查询条件*/
		if(StringUtils.isNotBlank(condition.getCustom_no())){
			sql.append("   AND LC.CUSTOM_NO LIKE ?");
		}
		/*本地交易流水号查询条件*/
		if(StringUtils.isNotBlank(condition.getLoc_trace_no())){
			sql.append("   AND LC.LOC_TRACE_NO LIKE ?");
		}
		/*渠道清算流水号查询条件*/
		if(StringUtils.isNotBlank(condition.getChannel_trace_no())){
			sql.append("   AND LC.CHANNEL_TRACE_NO LIKE ?");
		}
		sql.append(" ORDER BY LC.REC_CRT_TS DESC");
		return sql.toString();
	}
	
	/**
	 * 组装分页与导出查询条件
	 * @param condition
	 * @param customer
	 * @return
	 */
	private List<String> getChannelStlListParam(App_Log_ChannelStlEntity condition, App_Custom_InfEntity customer){
		List<String> params = new ArrayList<>();
		/*响应码查询条件*/
		if(SystemConstant.RSPCODE_ERROR.equals(condition.getRspcode())){
			params.add(SystemConstant.RSPCODE_SUCCESS);
			params.add(SystemConstant.RSPCODE_XX);
		}else if(StringUtils.isNotBlank(condition.getRspcode())){
			params.add(condition.getRspcode());
		}
		/*清算状态查询条件*/
		if(StringUtils.isNotBlank(condition.getStlst())){
			params.add(condition.getStlst());
		}
		/*渠道编号查询条件*/
		if(StringUtils.isNotBlank(condition.getChannel_no())){
			params.add(condition.getChannel_no());
		}
		/*申请结束日期查询条件,默认取系统当前日期,字段复用*/
		if(StringUtils.isBlank(condition.getLoc_ts())){
			params.add(DateUtil.getCurrDateStr());
		}else{
			params.add(condition.getLoc_ts().replaceAll("-", ""));
		}
		/*申请起始日期查询条件,默认取系统当前日期*/
		if(StringUtils.isBlank(condition.getLoc_dt())){
			params.add(DateUtil.getCurrDateStr());
		}else{
			params.add(condition.getLoc_dt().replaceAll("-", ""));
		}
		/*会员姓名查询条件*/
		if(StringUtils.isNotBlank(customer.getName())){
			params.add("%" + customer.getName() + "%");
		}
		/*会员登录账号查询条件*/
		if(StringUtils.isNotBlank(customer.getLogin_id())){
			params.add("%" + customer.getLogin_id() + "%");
		}
		/*会员编号查询条件*/
		if(StringUtils.isNotBlank(condition.getCustom_no())){
			params.add("%" + condition.getCustom_no() + "%");
		}
		/*本地交易流水号查询条件*/
		if(StringUtils.isNotBlank(condition.getLoc_trace_no())){
			params.add("%" + condition.getLoc_trace_no() + "%");
		}
		/*渠道清算流水号查询条件*/
		if(StringUtils.isNotBlank(condition.getChannel_trace_no())){
			params.add("%" + condition.getChannel_trace_no() + "%");
		}
		return params;
	}
	
	@PostConstruct
	private void init(){
		excelHead = new ArrayList<>();
		excelHead.add("申请时间");
		excelHead.add("请求序列号");
		excelHead.add("本地流水号");
		excelHead.add("会员编号");
		excelHead.add("登陆账号");
		excelHead.add("会员名称");
		excelHead.add("渠道编号-名称");
		excelHead.add("渠道方清算流水号");
		excelHead.add("易宝收款宝流水号");
		excelHead.add("申请金额");
		excelHead.add("手续费");
		excelHead.add("清算金额");
		excelHead.add("清算时间");
		excelHead.add("清算状态");
		excelHead.add("响应码-内容");
	}
}
